package com.apexsoft.margin.account.management.api.http;

import com.apexsoft.margin.account.management.constants.CommonConstants;
import com.apexsoft.margin.account.management.domain.excel.model.DistributionDataDomain;
import com.apexsoft.margin.account.management.domain.excel.model.RecycleDomain;
import com.apexsoft.margin.account.management.domain.excel.service.TemplateExcelRepository;
import com.apexsoft.margin.account.management.persistent.repository.DistributionExcelRepositoryImpl;
import com.apexsoft.margin.account.management.util.DealResp;
import com.apexsoft.margin.account.management.util.GsonUtil;
import com.apexsoft.margin.account.management.util.ResultUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 预分配（预锁）批量 controllrt
 *
 */
@RestController
@RequestMapping("/distribution")
public class DistributionExcelController {

    @Resource
    @Qualifier("DistributionExcelRepositoryImpl")
    TemplateExcelRepository<DistributionDataDomain> distributionExcelRepository;

    /**
     * 预分配（预锁）批量 controller
     * @param file
     * @return
     */
    @PostMapping("/distributionExcel")
    public ResultUtil uploadPreDisExcel(@RequestParam("file") MultipartFile file){
        ResultUtil result = new ResultUtil() ;
        result.setCode(CommonConstants.RESULT_CODE_SUCCESS);
        result.setNote("成功");
        try{
            String fileCode = UUID.randomUUID().toString() ;
            distributionExcelRepository.uploadExcel(file, fileCode);
            DealResp resp = new DealResp() ;
            List<DistributionDataDomain> list = distributionExcelRepository.getListDomainData(fileCode);
            result.setTotal(list.size());
            result.setData(resp.convertToHash(GsonUtil.getDefaultGson().toJson(list)));
        }catch (Exception e){
            result.setCode(CommonConstants.RESULT_CODE_ERROR) ;
            result.setNote("失败"+e.getMessage());
        }
        return result ;
    }


}
